<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\GiftLog;
use app\common\model\LaborLog;
use app\common\model\TeamLog;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

class Gift extends Api
{
    protected $noNeedLogin = ['list'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Gift;
    }

    //我收到礼物
    public function mylist()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',LIMIT);
        $user_id = $this->request->get('user_id');
        if(empty($user_id)) $user_id = $this->auth->id;

        $model = new GiftLog();
        $list = $model->with(['gift'])->where(['to_user_id' => $user_id])->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    //礼物列表
    public function list()
    {
        $list = $this->model->select();
        $this->success(__('成功'),$list);
    }

    //赠送礼物
    public function send()
    {
        $to_user_id = $this->request->get('to_user_id');
        $gift_id = $this->request->get('gift_id');

        if(empty($to_user_id)) $this->error('赠送人有误');

        $gift = $this->model::get($gift_id);

        if(empty($gift)) $this->error('赠送礼物错误');
        //判断用户有没有余额
        $user = $this->auth->getUser();
        if($user->money < $gift->price) {
            $this->error('当前余额不足，请立即充值','',3);
        }

        Db::startTrans();
        try {
            $model = new GiftLog();
            $model->save([
                'gift_id' => $gift_id,
                'user_id' => $this->auth->id,
                'to_user_id' => $to_user_id,
                'money' => $gift->price
            ]);

            $to_user = \app\common\model\User::get($to_user_id);
            $payamount = $gift->price;

            $switch = Config::get(['group' => 'example', 'name' => 'get_gift_ratio'])->value;
            $ratio = Config::get(['group' => 'example', 'name' => 'get_gift_ratio'])->value;

            //获得者推广者
            if(!empty($to_user->p_id) && !empty($switch)) {
                \app\common\model\UserIncome::money($payamount * ($ratio / 100),$to_user->p_id,'赠送礼物');
            }

            $send_gift_switch = Config::get(['group' => 'example', 'name' => 'send_gift_switch'])->value;
            $send_gift_ratio = Config::get(['group' => 'example', 'name' => 'send_gift_ratio'])->value;
            //赠送者推广者
            if(!empty($user->p_id) && !empty($send_gift_switch)) {
                \app\common\model\UserIncome::money($payamount * ($send_gift_ratio / 100),$user->p_id,'赠送礼物');
            }
            $memo = "{$user->nickname}向你赠送礼物，获得{$payamount}元宝";
            $memo1 = "你向{$to_user->nickname}赠送礼物";

            \app\common\model\Message::setMessage($this->auth->id,$memo1,'/pages/mine/gift');
            \app\common\model\Message::setMessage($to_user_id,$memo,'/pages/mine/gift');
            $gift_ratio = Config::get(['group' => 'example', 'name' => 'gift_ratio'])->value;

            \app\common\model\User::money(-$payamount,$this->auth->id,$memo1);
            \app\common\model\User::money($payamount * ($gift_ratio  / 100),$to_user_id,$memo);

            Db::commit();
        }catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success(__('赠送成功'));

    }
}